﻿/********************************************************************************
** auth： Kadu
** date： 2018/01/13
** desc： 读取二进制数据
** Ver.:  V1.0.0
*********************************************************************************/


using System.IO;

namespace SimpleFramework
{
    /// <summary>
    /// 读取字节
    /// </summary>
    public class DataBufferReader
    {
        private int mPick = 0;
        private int mSize = 0;
        private byte[] mBuffer;
        //private Stream mStream;

        public DataBufferReader(byte[] buffer)
        {
            mBuffer = buffer;
            mSize = mBuffer.Length;
            mPick = 0;
        }

        public void Reset()
        {
            mPick = 0;
        }

        public sbyte ReadInt8()
        {
            if (mPick + 1 <= mSize)
            {
                return (sbyte)mBuffer[mPick++];
            }
            else
            {
                return 0;
            }
        }

        public short ReadInt16()
        {
            if (mPick + 2 <= mSize)
            {
                short value = System.BitConverter.ToInt16(mBuffer, mPick);
                mPick += 2;
                return value;
            }
            else
            {
                return 0;
            }

        }

        public int ReadInt32()
        {
            if (mPick + 4 <= mSize)
            {
                int value = System.BitConverter.ToInt32(mBuffer, mPick);
                mPick += 4;
                return value;
            }
            else
            {
                return 0;
            }
        }

        public long ReadInt64()
        {
            if (mPick + 8 <= mSize)
            {
                long value = System.BitConverter.ToInt64(mBuffer, mPick);
                mPick += 8;
                return value;
            }
            else
            {
                return 0;
            }
        }

        public byte ReadUInt8()
        {
            if (mPick + 1 <= mSize)
            {
                return mBuffer[mPick++];
            }
            else
            {
                return 0;
            }
        }

        public ushort ReadUInt16()
        {
            if (mPick + 2 <= mSize)
            {
                ushort value = System.BitConverter.ToUInt16(mBuffer, mPick);
                mPick += 2;
                return value;
            }
            else
            {
                return 0;
            }
        }

        public uint ReadUInt32()
        {
            if (mPick + 4 <= mSize)
            {
                uint value = System.BitConverter.ToUInt32(mBuffer, mPick);
                mPick += 4;
                return value;
            }
            else
            {
                return 0;
            }
        }

        public ulong ReadUInt64()
        {
            if (mPick + 8 <= mSize)
            {
                ulong value = System.BitConverter.ToUInt64(mBuffer, mPick);
                mPick += 8;
                return value;
            }
            else
            {
                return 0;
            }
        }

        public float ReadSingle()
        {
            if (mPick + 4 <= mSize)
            {
                float value = System.BitConverter.ToSingle(mBuffer, mPick);
                mPick += 4;
                return value;
            }
            else
            {
                return 0;
            }
        }

        public double ReadDouble()
        {
            if (mPick + 8 <= mSize)
            {
                double value = System.BitConverter.ToDouble(mBuffer, mPick);
                mPick += 8;
                return value;
            }
            else
            {
                return 0;
            }
        }

        public string ReadString()
        {
            int count = this.ReadUInt16();
            if (mPick + count <= mSize)
            {
                string value = System.Text.Encoding.UTF8.GetString(mBuffer, mPick, count);
                mPick += count;
                return value;
            }
            else
            {
                return string.Empty;
            }
        }

        public int ReadArrayCount()
        {
            return this.ReadInt32();
        }

        public sbyte[] ReadInt8Array()
        {
            int count = this.ReadArrayCount();
            sbyte[] values = new sbyte[count];
            for (int i = 0; i < count; ++i)
                values[i] = this.ReadInt8();
            return values;
        }

        public short[] ReadInt16Array()
        {
            int count = this.ReadArrayCount();
            short[] values = new short[count];
            for (int i = 0; i < count; ++i)
                values[i] = this.ReadInt16();
            return values;
        }

        public int[] ReadInt32Array()
        {
            int count = this.ReadArrayCount();
            int[] values = new int[count];
            for (int i = 0; i < count; ++i)
                values[i] = this.ReadInt32();
            return values;
        }

        public long[] ReadInt64Array()
        {
            int count = this.ReadArrayCount();
            long[] values = new long[count];
            for (int i = 0; i < count; ++i)
                values[i] = this.ReadInt64();
            return values;
        }

        public byte[] ReadUInt8Array()
        {
            int count = this.ReadArrayCount();
            byte[] values = new byte[count];
            for (int i = 0; i < count; ++i)
                values[i] = this.ReadUInt8();
            return values;
        }

        public ushort[] ReadUInt16Array()
        {
            int count = this.ReadArrayCount();
            ushort[] values = new ushort[count];
            for (int i = 0; i < count; ++i)
                values[i] = this.ReadUInt16();
            return values;
        }

        public uint[] ReadUInt32Array()
        {
            int count = this.ReadArrayCount();
            uint[] values = new uint[count];
            for (int i = 0; i < count; ++i)
                values[i] = this.ReadUInt32();
            return values;
        }

        public ulong[] ReadUInt64Array()
        {
            int count = this.ReadArrayCount();
            ulong[] values = new ulong[count];
            for (int i = 0; i < count; ++i)
                values[i] = this.ReadUInt64();
            return values;
        }

        public float[] ReadSingleArray()
        {
            int count = this.ReadArrayCount();
            float[] values = new float[count];
            for (int i = 0; i < count; ++i)
                values[i] = this.ReadSingle();
            return values;
        }

        public double[] ReadDoubleArray()
        {
            int count = this.ReadArrayCount();
            double[] values = new double[count];
            for (int i = 0; i < count; ++i)
                values[i] = this.ReadDouble();
            return values;
        }

        public string[] ReadStringArray()
        {
            int count = this.ReadArrayCount();
            string[] values = new string[count];
            for (int i = 0; i < count; ++i)
                values[i] = this.ReadString();
            return values;
        }
    }

}

